//公告栏自适应高度
function fixedHeight($e) {
    var $side=$e;
    $li = $side.next();
    var sideHeight = $side[0].offsetHeight;
    var liHeight = $li[0].offsetHeight;
    $side.height(Math.ceil(sideHeight/liHeight)*liHeight);
}

//回顶部
function showTop($e) {
    var $top = $e;
    var $body = $("body");
    var width = $body.width();
    var scrollTop = $(document).scrollTop() || $(window).scrollTop();
    var left = width * 0.9;
    var top = scrollTop+400;
    if(scrollTop>500) {
        $top.show();
        $top.css("left",left+"px");
        $top.css("top",top+"px");
    } else {
        $top.hide();
    }
}

function setTip(t,o,o1) {
    o.children("span").text(t);
    o.removeClass("error").addClass("info");
    var p = o1.position();
    if(!o.hasClass("jf")) {
        o.css({
            left:(p.left+o1.innerWidth()+11)+"px"
        })
    }

    o.show();
}


function setError(t,o,o1) {
    setTip(t,o,o1);
    o.removeClass("info").addClass("error");
}

$.fn.filedTip = function(o) {
    var oldColor = this.css("color");
    o = $.extend({
        color:"gray",
        tip:""
    },o)
    //o.tip //o.css

    return this.each( function() {
        $(this).val(o.tip).css("color",o.color);
        $(this).bind("focus", function() {
            var val = $(this).val();
            if(val == o.tip) {
                $(this).val("").css("color",oldColor);
            }
        }).bind("blur", function() {
            var val = $(this).val();
            if(val == "") {
                $(this).val(o.tip).css("color",o.color);
            }
        });
    });
}
//加载图片
function loadItemBoxes() {
    var $listItems = $(".listItems");
    var $wait = $(".wait");
    var newLis =[];
    $listItems.find("li").slice(0,15).each( function() {
        var $li = $(this);
        newLis.push($li.clone());
    });
    for(var i =0;i<newLis.length; i++) {
        $listItems.append(newLis[i]);
    }
    /*$(".listItems>li:gt(18)").css("margin-right","3px");*/
    $wait.hide();
}

function toggleLangArea() {
    if($("#langArea").css("display")=="none") {
        showLangArea();
    } else {
        closeLangArea();
    }
}

function showLangArea() {
    if($("#langArea").data("loaded")) {
        $("#langArea").fadeIn();
    } else {
        $.ajax({
            type: "get",
            url: "lang.html",
            dataType: "html",
            beforeSend: function() {
                $("#loading").show().delay(500);
            },
            complete: function() {
                $("#loading").hide();
            },
            success: function(html) {

                $("#langInner").append(html);
                var h = (-$("#langArea").outerHeight(true))/2;
                if($.browser.msie) {
                    h -= 5;
                }
                $("#langArea").css({
                    "margin-left":"-305px",
                    "margin-top":h+"px"
                }).fadeIn();
                $("#langArea").data("loaded",true);
            }
        });
    }
}

function copyToClipboard(txt) {
    if(window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if(navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans,null,clipid.kGlobalClipboard);
        alert("复制成功！")
    }
}

function closeLangArea() {
    $("#loading").hide();
    $("#langArea").hide();
}

// 判断一个节点是否为子节点
function contains(parentNode, childNode) {
    if (parentNode.contains) {
        return parentNode != childNode && parentNode.contains(childNode);
    } else {
        return !!(parentNode.compareDocumentPosition(childNode) & 16);
    }
}

//屏蔽IE在容器内部的多次触发mouseover和mouseout事件
function fixedMouse(e,target) {
    var related,
    type=e.type.toLowerCase();//这里获取事件名字
    if(type=='mouseover') {
        related=e.relatedTarget||e.fromElement
    } else if(type='mouseout') {
        related=e.relatedTarget||e.toElement
    } else
        return true;
    return related && related.prefix!='xul' && !contains(target,related) && related!==target;
}

$.isIE6 = function() {
    if($.browser.msie&&$.browser.version=="6.0") {
        return true;
    }
    return false;
}
function getFlashVer() {
    var f=null;
    var n=navigator;
    if (n.plugins && n.plugins.length) {
        for (var ii=0;ii<n.plugins.length;ii++) {
            if (n.plugins[ii].name.indexOf('Shockwave Flash')!=-1) {
                f=n.plugins[ii].description.split('Shockwave Flash ')[1].split(' ')[0];
                break;
            }
        }
    } else if (window.ActiveXObject) {
        for (var ii=10;ii>=2;ii--) {
            try {
                var fl=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+ii+"');");
                if (fl) {
                    f=ii + '.0';
                    break;
                }
            } catch(e) {
            }
        }
    }
    return f;
}

function getIEVersion() {
    if(!$.browser.msie)
        return null;
    return parseInt($.browser.version);
}

function isEmail(str) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    return reg.test(str);
}

function isPassword(str) {
    var reg = /^[a-zA-Z0-9_]{9,30}$/;
    return reg.test(str);
}

function getStrLen(str) {
    return str.replace(/[^\u0000-\u00ff]/g,"aa").length;
}

window.onload = function() {

    if($.isIE6()) {
        $(".mainNav .leftNav li,.mainNav .rightNav li,.listItems .image img,.langContainer .lang li .item").hover( function() {
            $(this).addClass("hover");
        }, function() {
            $(this).removeClass("hover");
        });
    }
    if(!getFlashVer()) {

        $("embed").before("<table class='flashwarning'>"+
        "<tbody>"+
        "<tr valign='middle'>"+
        "<td align='center'>"+
        "<div class='icon box-alert'></div>"+

        "您需要先升级Adobe Flash Player 才能观看此视频<a href='#'>从Adobe下载</a>"+

        "</td>"+

        "</tr>"+
        "</tbody>"+

        "</table>").hide();
    }
    $(".headLeftText a").click( function() {
        toggleLangArea();
    });
    /*$(".grayscale").each( function() {
     var img = $(this).find("img");
     var w = img.innerWidth();
     var h = img.innerHeight();
     var l = (img.outerWidth(true)-w)/2;
     var t = (img.outerHeight(true)-h)/2;
     $(this).append($("<s></s>").width(w).height(h).css({
     "position":"absolute",
     "left":img.position().left+l+"px",
     "top":img.position().top+t+"px",
     "background":"#000",
     "filter":"alpha(opacity=50)",
     "opacity":"0.5"
     })).css({
     "position":"relative"
     }).hover( function() {
     $(this).addClass("hover");
     }, function() {
     $(this).removeClass("hover");
     });
     });*/
    $("img.grayscale").each( function() {
        $img = $(this);
        var cls = $(this).parent().attr("class");
        $("<div></div>").attr("style","display:inline-block;*display:inline;*zoom:1;position:relative;")
        .appendTo($(this).parent()).append($(this))
        .append($("<s></s>").attr("style","position:absolute;width:100%;height:100%;left:0;top:0;filter:alpha(opacity=50);opacity:0.5;background:#000"))
        .hover( function() {
            $(this).children("img").addClass("hover");
        }, function() {
            $(this).children("img").removeClass("hover");
        }).css("cursor","pointer");
        if(cls == "image") {
            var s = $(this).siblings("s");
            $(this).siblings("s").width(s.width()-6).height(s.height()-6).css({
                left:"3px",
                top:"3px"
            });
        }
    });
    $("img.closable").each( function() {
        var $a = $("<a>X</a>").attr("style","color:#000;text-decoration:none;font-weight:bold;position:absolute;right:10px;top:10px;filter:alpha(opacity=50);opacity:0.5;z-index:99;display:none;").bind("click", function() {
            $(this).closest("li").remove();
        });
        if($(this).hasClass("grayscale")) {
            $(this).after($a.css("color","#fff")).parent().hover( function() {
                $(this).children("a").show();
            }, function() {
                $(this).children("a").hide();
            });
        } else {
            $("<div></div>").attr("style","display:inline-block;*display:inline;*zoom:1;position:relative;*zoom:1;")
            .appendTo($(this).parent()).append($(this))
            .append($a)
            .hover( function() {
                $(this).children("img").addClass("hover");
                $(this).children("a").show();
            }, function() {
                $(this).children("img").removeClass("hover");
                $(this).children("a").hide();
            }).css("cursor","pointer");
        }

    });
};
